package org.ourgrid.acceptance.util.discoveryservice;

import org.easymock.classextension.EasyMock;
import org.ourgrid.acceptance.util.DiscoveryServiceAcceptanceUtil;
import org.ourgrid.common.interfaces.DiscoveryService;
import org.ourgrid.common.interfaces.DiscoveryServiceClient;
import org.ourgrid.common.interfaces.RemoteWorkerProvider;
import org.ourgrid.common.interfaces.status.PeerStatusProvider;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.discoveryservice.communication.receiver.DiscoveryServiceReceiver;

import br.edu.ufcg.lsd.commune.container.ObjectDeployment;
import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;
import br.edu.ufcg.lsd.commune.context.ModuleContext;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.identification.ServiceID;
import br.edu.ufcg.lsd.commune.testinfra.AcceptanceTestUtil;
import br.edu.ufcg.lsd.commune.testinfra.util.TestStub;

public class Req_505_Util extends DiscoveryServiceAcceptanceUtil {

	public Req_505_Util(ModuleContext context) {
		super(context);
	}

	public void getRemoteWorkerProviders(DiscoveryServiceComponent component, TestStub dscTestStub, boolean isLogged) {
		DiscoveryService discoveryService = (DiscoveryService) getDiscoveryServiceProxy();
		ObjectDeployment dsOD = getDiscoveryServiceControlDeployment(component);
		
//		DeploymentID dsClientID = new DeploymentID(dscTestStub.getDeploymentID().getContainerID(), PeerConstants.DS_CLIENT);
//		DiscoveryServiceClient dsClient = EasyMock.createMock(DiscoveryServiceClient.class);

		DiscoveryServiceClient dsc = (DiscoveryServiceClient) dscTestStub.getObject();
		
		AcceptanceTestUtil.publishTestObject(component, dscTestStub.getDeploymentID(), dsc, DiscoveryServiceClient.class);
		
//		if (pspTestStub != null) {
//			AcceptanceTestUtil.publishTestObject(component, pspTestStub.getDeploymentID(), 
//					(PeerStatusProvider) pspTestStub.getObject(), PeerStatusProvider.class);
//		}
		
//		AcceptanceTestUtil.publishTestObject(component, dsClientID, dsClient, DiscoveryServiceClient.class);
		AcceptanceTestUtil.setExecutionContext(component, dsOD, dscTestStub.getDeploymentID());

		CommuneLogger logger = component.getLogger();
		
		if (!isLogged) {
			logger.debug("The client [" + dscTestStub.getDeploymentID().getContainerID().getUserAtServer() + "] is not logged.");
			EasyMock.replay(logger);
		}
		
		discoveryService.getRemoteWorkerProviders(dsc);
		
		if (!isLogged) {
			EasyMock.verify(logger);
			EasyMock.reset(logger);
		}
		
//		AcceptanceTestUtil.notifyRecovery(component, rwpTestStub.getDeploymentID());
//		DiscoveryServiceController controller = (DiscoveryServiceController) getDiscoveryServiceDeployment(component).getObject();
//		controller.remoteWorkerProviderIsUp(rwp, rwpTestStub.getDeploymentID());
		
//		if (pspTestStub != null) {
//			AcceptanceTestUtil.notifyRecovery(component, pspTestStub.getDeploymentID());
//			controller.peerStatusProviderIsUp((PeerStatusProvider) pspTestStub.getObject(), 
//					pspTestStub.getDeploymentID());
//		}
		
//		return new TestStub(dsClientID, dsClient);
	}
	
	public void getRemoteWorkerProviders(DiscoveryServiceComponent component, TestStub dscTestStub) {
		getRemoteWorkerProviders(component, dscTestStub, false);
	}
	
	public void notifyDiscoveryServiceClientFailure(DiscoveryServiceComponent component, TestStub dscTestStub) {
		component.getContainer().setStubDown(dscTestStub.getObject());
		DiscoveryServiceReceiver controller = (DiscoveryServiceReceiver) getDiscoveryServiceDeployment(component).getObject();
		controller.dsClientIsDown((DiscoveryServiceClient) dscTestStub.getObject(), dscTestStub.getDeploymentID());
	}
	
	public TestStub createRemoteWorkerProvider(ServiceID serviceID){
		DeploymentID rwpID = new DeploymentID(serviceID);
		RemoteWorkerProvider rwp = EasyMock.createMock(RemoteWorkerProvider.class);
		return new TestStub(rwpID, rwp);
	}
	
	public TestStub createDiscoveryServiceClient(ServiceID serviceID){
		DeploymentID dscID = new DeploymentID(serviceID);
		DiscoveryServiceClient rwp = EasyMock.createMock(DiscoveryServiceClient.class);
		return new TestStub(dscID, rwp);
	}
	
	public TestStub createPeerStatusProvider(ServiceID serviceID){
		DeploymentID peerStatusProviderID = new DeploymentID(serviceID);
		PeerStatusProvider peerStatusProvider = EasyMock.createMock(PeerStatusProvider.class);
		return new TestStub(peerStatusProviderID, peerStatusProvider);
	}
}
